﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Composite.Regions;
using EQual.Infrastructure;

namespace EQual.MainModule.Employees.Presenters
{

    public class WorkExperianceController : EQual.Infrastructure.MVPFramework.Presenters.ControllerBase<DetailsViews .EmployeesExperianceView , DataAccesLayer.Employee>,EQual.Infrastructure.IModuleController
    {

        DataAccesLayer.EmployeeDataAccesLayer accesLayer;
        EQual.MainModule.DetailsContainer containerView = new DetailsContainer();

        public DataAccesLayer.EmployeeDataAccesLayer AccesLayer
        {
            get { return accesLayer; }
            set { accesLayer = value; }
        }

        IRegionManager _regionManager;

        DetailsViews .EmployeesExperianceView  _view_Main;

        public DetailsViews.EmployeesExperianceView View
        {
            get { return _view_Main; }
            set { _view_Main = value; }
        }

        public WorkExperianceController(IRegionManager regionManager)
            : this()
        {
            this._regionManager = regionManager;

        }

        public WorkExperianceController()
        {
            accesLayer = new DataAccesLayer.EQualDataLayerFactory().GetEmployeeAccesLayer();
            InitializeViews();

        }

        //secondary Presenters and Views 


        EQual .Infrastructure .MVPFramework .Models .DefaulPresentationModel <DataAccesLayer .Work_Experiance > ViewModel;

        public override void Dispaly(DataAccesLayer.Employee item)
        {

            base.Dispaly(item);
        }

        public void Run()
        {
            ViewModel = new EQual.Infrastructure.MVPFramework.Models.DefaulPresentationModel<DataAccesLayer.Work_Experiance>();

            ViewModel.ItemsCollectionProvider = delegate()
            {
                //return new DataAccesLayer.EQualDataLayerFactory().GetEmployeeAccesLayer().GetEmployeeExperience( as DataAccesLayer.Employee);
                return null;
            };

            ViewModel.ModelCollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(ViewModel_ModelCollectionChanged);
        
            View = new EQual.MainModule.Employees.DetailsViews.EmployeesExperianceView(); 

            View.Model = ViewModel;

            View.ItemSelected += new EventHandler<Microsoft.Practices.Composite.Events.DataEventArgs<object>>(View_ItemSelected);
            View.ItemDeleted += new EventHandler<Microsoft.Practices.Composite.Events.DataEventArgs<object>>(View_ItemDeleted);
            View.ItemAdded += new EventHandler<Microsoft.Practices.Composite.Events.DataEventArgs<object>>(View_ItemAdded);

            this._regionManager.RegisterViewWithRegion(RegionNames.MainRegion, () => this.View);
            this._regionManager.Regions[RegionNames.MainRegion].Activate(this.View);
        }

        void ViewModel_ModelCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {

        }

        void View_ItemAdded(object sender, Microsoft.Practices.Composite.Events.DataEventArgs<object> e)
        {
            RequestNewItemCreation(e.Value as DataAccesLayer.Employee);
        }

        void View_ItemSelected(object sender, Microsoft.Practices.Composite.Events.DataEventArgs<object> e)
        {
            InjectViews();
            if (e.Value == null) return;

   
        }

        void View_ItemDeleted(object sender, Microsoft.Practices.Composite.Events.DataEventArgs<object> e)
        {
            RequestItemDelete(e.Value as DataAccesLayer.Employee);
        }

        protected virtual void InjectViews()
        {
            //throw in to the datails region the datails view
            IRegion region = _regionManager.Regions[RegionNames.ActionRegion];

            if (!region.Views.Contains(containerView))
            {
 
                region.Add(containerView);
            }

            //activate the current view
            region.Activate(containerView);
        }

        public object GetDecoratedView(object view, string header)
        {
            return new DataGridDecorator(view, header);
        }


        public void SubmitChanges()
        {

        }

        public void RequestNewItemCreation(DataAccesLayer.Employee empl)
        {

        }

        public void RequestItemDelete(DataAccesLayer.Employee empl)
        {

        }

        internal void RequestItemDelete(System.Collections.IList iList)
        {

        }

    }
 
}
